@page "/register"
@attribute [AllowAnonymous]
@using TaskoMask.BuildingBlocks.Contracts.Dtos.Owners
@inject OwnerApiService OwnerApiService

<section id="cover">
    <div id="cover-caption">
        <div class="container">
            <div class="row text-white">
                <div class="col-xl-5 col-lg-6 col-md-8 col-sm-10 mx-auto text-center form p-4">
                    <h3 class=" py-2 ">Register in TaskoMask!</h3>
                    <div class="px-2">
                        @if (IsProcessing)
                        {
                            <div class="spinner-loading"></div>
                        }
                        else
                        {
                            <EditForm Model="RegisterModel" OnValidSubmit="HandleRegister">

                                <DataAnnotationsValidator />

                                <div class="form-group">
                                    <InputText @bind-Value="RegisterModel.DisplayName" placeholder="DisplayName" class="form-control"></InputText>
                                    <ValidationMessage For="()=>RegisterModel.DisplayName" class="text-danger"></ValidationMessage>
                                </div>
                                <div class="form-group">
                                    <InputText @bind-Value="RegisterModel.Email" placeholder="Email" class="form-control"></InputText>
                                    <ValidationMessage For="()=>RegisterModel.Email" class="text-danger"></ValidationMessage>
                                </div>
                                <div class="form-group">
                                    <InputText type="password" @bind-Value="RegisterModel.Password" placeholder="Password" class="form-control"></InputText>
                                    <ValidationMessage For="()=>RegisterModel.Password" class="text-danger"></ValidationMessage>
                                </div>
                                <div class="form-group">
                                    <InputText type="password" @bind-Value="RegisterModel.ConfirmPassword" placeholder="ConfirmPassword" class="form-control"></InputText>
                                    <ValidationMessage For="()=>RegisterModel.ConfirmPassword" class="text-danger"></ValidationMessage>
                                </div>
                                <div class="form-group">
                                    <button id="registerBtn" type="submit" class="btn btn-success">Register</button>
                                    <a id="loginBtn" class="btn btn-outline-primary" href="authentication/login">Login</a>
                                </div>
                            </EditForm>
                        }


                    </div>
                </div>
            </div>
        </div>
    </div>
</section>


@code {


    #region Fields and Parameters


    /// <summary>
    /// Form Model
    /// </summary>
    RegisterOwnerDto RegisterModel = new RegisterOwnerDto();


    bool IsProcessing = false;


    #endregion Fields and Parameters




    #region private Methods


    /// <summary>
    ///
    /// </summary>
    private async Task HandleRegister()
    {
        IsProcessing = true;
        var registerResult = await OwnerApiService.RegisterAsync(RegisterModel);
        if (registerResult.IsSuccess)
        {
            ComponentMessageService.SendMessage(MessageType.Owner_Registered);
            NavigationManager.NavigateTo("/");
         }
        else
            ToastService.ShowError(registerResult.Errors.ParseToFragment(), registerResult.Message);
        IsProcessing = false;
    }



    #endregion private Methods

}